package com.igoal.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.UserDTO;
import com.igoal.dto.UserGoalDTO;
import com.igoal.service.GoalManager;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.ConvertUtils;

@Controller
@RequestMapping("/ajax/secure/addGoalResult.html")
public class AjaxAddGoalResultController extends BaseAjaxController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap, @RequestParam("goalId") String sGoalId) {
		Long lGoalId = ConvertUtils.toLong(sGoalId, -1L);
		if (lGoalId > 0L) {
			UserDTO user = getUserSession();
			UserGoalDTO model = userGoalManager.getUserGoalByPrimaryKey(user.getId(), lGoalId);
			if (model != null) {
				List<UserGoalDTO> data = new ArrayList<UserGoalDTO>();
				data.add(model);
				applyModel(modelMap, data);
			}
		}

		return getViewPrefix() + "addGoalResult";
	}
}
